<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller
{
	
	public function __construct()
	{
		parent::__construct();
		$this->load->helper(array('url','form','url_encrypt'));
		$this->load->library(array('form_validation','session'));
	}
	public function view_admin()
	{
		$this->load->model('m_view','m');
		$data['data']['system_message'] = $this->session->flashdata('system_message');
		$data['data']['admin'] = $this->m->get_admin();
		$data['page'] = 'view_admin';
		$this->load->view('template',$data);

	}
	
	public function view_teaching()
	{
		$data['data']['system_message'] = $this->session->flashdata('system_message');
		$this->load->model('m_view','m');
		$data['data']['teach'] =  $this->m->get_teach();
		$data['page'] = 'view_teach';
		$this->load->view('template',$data);
	}
	
	public function add_admin()
	{
		$data['data']['system_message'] = $this->session->flashdata('system_message');
		$data['page'] = 'add_admin';
		
		if($this->form_validation->run('admin') == FALSE)
		{
			$this->load->view('template',$data);
		}else
		{
			$input['firstname'] = $this->input->post('firstname');
			$input['middlename'] = $this->input->post('middlename');
			$input['lastname'] = $this->input->post('lastname');
			$input['age'] = $this->input->post('age');
			$input['gender'] = $this->input->post('gender');
			$input['baccalaureate'] = $this->input->post('baccalaureate');
			$input['masters'] = $this->input->post('masters');
			$input['licensure_exams_taken'] = $this->input->post('licensure');
			$input['employment_stat'] = $this->input->post('emp_stat');
			$input['position'] = $this->input->post('position');
			$input['work_exp'] = $this->input->post('work_exp');

			$this->load->model('m_insert');
			if($this->m_insert->insert_admin($input))
			{
				$this->session->set_flashdata('system_message','<div class="alert alert-success"><i class="icon-ok"></i>Add was successfull</div>');
				redirect('home/add_admin');
			}else{
				$data['data']['system_message'] = '<div class="alert alert-error">An Error has been encountered</div>';
				$this->load->view('template',$data);
			}
		}
	}
	
	public function add_teach()
	{
		$data['data']['system_message'] = $this->session->flashdata('system_message');
		$data['page'] = 'add_teach';
		

		if($this->form_validation->run('teach') == false)
		{
			$this->load->view('template',$data);
		}else{

			$input['firstname'] = $this->input->post('firstname');
			$input['middlename'] = $this->input->post('middlename');
			$input['lastname'] = $this->input->post('lastname');
			$input['age'] = $this->input->post('age');
			$input['gender'] = $this->input->post('gender');
			$input['baccalaureate'] = $this->input->post('baccalaureate');
			$input['masters'] = $this->input->post('masters');
			$input['licensure_exams_taken'] = $this->input->post('licensure_exams_taken');
			$input['employment_stat'] = $this->input->post('employment_stat');
			$input['work_exp_teaching'] = $this->input->post('teaching_work_exp');
			$input['work_exp_admin'] = $this->input->post('admin_work_exp');
			$input['subjects_handled'] = $this->input->post('subj_handled');

			$this->load->model('m_insert');
			if($this->m_insert->new_teaching_personnel($input))
			{
				$this->session->set_flashdata('system_message','<div class="alert alert-success">Add was successfull</div>');
				redirect('home/add_teach');
			}else{
				$data['data']['system_message'] = '<div class="alert alert-error">An Error has been encountered</div>';
				$this->load->view('template',$data);
			}		
		}
	}

	public function del_admin()
	{
		$id = $this->input->get('id');
		$di = $this->input->get('di');
		$result = _sd($id,$di);

		if($result->status == 'TRUE')
		{
			$this->load->model('m_delete','m');
			if($this->m->delete_admin($result->link))
			{
				$this->session->set_flashdata('system_message','<div class="alert alert-success"><i class="icon-ok"></i> Delete was successfull</div>');
				redirect('home/view_admin');
			}else{
				$this->session->set_flashdata('system_message','<div class="alert alert-error"><i class="icon-warning-sign"></i> Encountered an error while trying to delete user</div>');
				redirect('home/view_admin');
			}
		}else{
			show_404();
		}
	}

	public function del_teach()
	{
		$id = $this->input->get('id');
		$di = $this->input->get('di');
		$result = _sd($id,$di);

		if($result->status == 'TRUE')
		{
			$this->load->model('m_delete','m');
			if($this->m->delete_teacher($result->link))
			{
				$this->session->set_flashdata('system_message','<div class="alert alert-success"><i class="icon-ok"></i> Delete was successfull</div>');
				redirect('home/view_teaching');
			}else{
				$this->session->set_flashdata('system_message','<div class="alert alert-error"><i class="icon-warning-sign"></i> Encountered an error while trying to delete user</div>');
				redirect('home/view_teaching');
			}
		}else{
			show_404();
		}
	}


	public function edit_admin()
	{	
		$id = $this->input->get('id');
		$di = $this->input->get('di');
		$result = _sd($id,$di);
		$data['data']['system_message'] = $this->session->flashdata('system_message');
		$this->load->model('m_view','m');
		
		$data['page'] = strtolower(__function__);
		
		if($this->session->userdata('updateAdminProfile') == false)
		{
			if($result->status == 'TRUE')
			{
				$data['data']['admin_profile'] = $this->m->get_admin($result->link);
				$this->session->set_userdata('updateAdminProfile',$result->link);
				$this->load->view('template',$data);
			}else
			{
				show_404();
			}
		}else{
			if($this->form_validation->run('admin') == false)
			{
				$data['data']['admin_profile'] = $this->m->get_admin($this->session->userdata('updateAdminProfile'));
				$this->load->view('template',$data);
			}else{

		    	$input['firstname'] = $this->input->post('firstname',TRUE);
		    	$input['middlename'] = $this->input->post('middlename',TRUE);
		    	$input['lastname'] = $this->input->post('lastname',TRUE);
		    	$input['age'] = $this->input->post('age',TRUE);
		    	$input['gender'] = $this->input->post('gender',TRUE);
		    	$input['baccalaureate'] = $this->input->post('baccalaureate',TRUE);
		    	$input['masters'] = $this->input->post('masters',TRUE);
		    	$input['licensure_exams_taken'] = $this->input->post('licensure',TRUE);
		    	$input['employment_stat'] = $this->input->post('emp_stat',TRUE);
		    	$input['work_exp'] = $this->input->post('work_exp',TRUE);
		    	$input['position'] = $this->input->post('position',TRUE);
    			$ae_id = $this->session->userdata('updateAdminProfile');

    			$this->load->model('m_update','mu');
    			if($this->mu->update_admin($input,$ae_id))
    			{
    				$this->session->set_userdata('updateAdminProfile','');
					$this->session->set_flashdata('system_message','<div class="alert alert-success"><i class="icon-ok"></i> Update was successfull</div>');
					redirect('home/view_admin');    				
    			}else{
					$this->session->set_flashdata('system_message','<div class="alert alert-error"><i class="icon-ok"></i> Error encountered while processing request</div>');
					redirect('home/edit_admin');
    			}
			}
		}
	}

	public function edit_teach()
	{	
		$id = $this->input->get('id');
		$di = $this->input->get('di');
		$result = _sd($id,$di);
		$data['data']['system_message'] = $this->session->flashdata('system_message');
		$this->load->model('m_view','m');
		
		$data['page'] = strtolower(__function__);
		
		if($this->session->userdata('updateTeachProfile') == false)
		{
			if($result->status == 'TRUE')
			{
				$data['data']['teach_profile'] = $this->m->get_teach($result->link);
				$this->session->set_userdata('updateTeachProfile',$result->link);
				$this->load->view('template',$data);
			}else
			{
				show_404();
			}
		}else{
			if($this->form_validation->run('teach') == false)
			{
				$data['data']['teach_profile'] = $this->m->get_teach($this->session->userdata('updateTeachProfile'));
				$this->load->view('template',$data);
			}else{
		    	$input['firstname'] = $this->input->post('firstname',TRUE);
		    	$input['middlename'] = $this->input->post('middlename',TRUE);
		    	$input['lastname'] = $this->input->post('lastname',TRUE);
		    	$input['age'] = $this->input->post('age',TRUE);
		    	$input['gender'] = $this->input->post('gender',TRUE);
		    	$input['baccalaureate'] = $this->input->post('baccalaureate',TRUE);
		    	$input['masters'] = $this->input->post('masters',TRUE);
		    	$input['licensure_exams_taken'] = $this->input->post('licensure_exams_taken',TRUE);
		    	$input['employment_stat'] = $this->input->post('employment_stat',TRUE);
		    	$input['subjects_handled'] = $this->input->post('subj_handled',TRUE);
		    	$input['work_exp_admin'] = $this->input->post('admin_work_exp',TRUE);
		    	$input['work_exp_teaching'] = $this->input->post('teaching_work_exp',TRUE);
    			$tp_id = $this->session->userdata('updateTeachProfile');

    			$this->load->model('m_update','mu');
    			if($this->mu->update_teach($input,$tp_id))
    			{
    				$this->session->set_userdata('updateTeachProfile','');
					$this->session->set_flashdata('system_message','<div class="alert alert-success"><i class="icon-ok"></i> Update was successfull</div>');
					redirect('home/view_teaching');    				
    			}else{
					$this->session->set_flashdata('system_message','<div class="alert alert-error"><i class="icon-ok"></i> Error encountered while processing request</div>');
					redirect('home/edit_teach');
    			}
			}
		}
	}


}
/* End of file post.php */
/* Location: ./application/controllers/welcome.php */